﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace market
{
    public partial class orderManage : System.Web.UI.Page
    {
        CommonClass ccObj = new CommonClass();
        DBClass dbObj = new DBClass();
        protected DataRowCollection OrderItems = null;
        public static OrderProperty order = new OrderProperty();
        protected void Page_Load(object sender, EventArgs e)
        {
            order = GetOrderInfo();
            if (!IsPostBack)
            {
                check_Login();
                ModifyBind();//显示订单状态
            }
        }
        public void check_Login()
        {
            if ((Session["AName"] == null))
            {
                Response.Write("<script>alert('对不起！您不是管理员，无权限浏览此页！');location='Login.aspx'</script>");
                Response.End();
            }
        }
        // 绑定订单状态
        public void ModifyBind()
        {
            string strSql = "select IsConfirm,IsSend from OrderInfo where OrderID=" + Convert.ToInt32(Request["OrderID"].Trim());
            DataTable dsTable = dbObj.GetDataSetStr(strSql, "tbOI");
            this.chkConfirm.Checked = Convert.ToBoolean(dsTable.Rows[0][0]);    //是否被确认
            this.chkConsignment.Checked = Convert.ToBoolean(dsTable.Rows[0][1]);//是否已发货
            if (this.chkConfirm.Checked == false)
            {
                this.chkConsignment.Visible = false;
            }
            else
            {
                if (this.chkConsignment.Checked == false)
                {
                    this.chkConfirm.Enabled = false;
                }
                else
                {
                    this.btnSave.Visible = false;
                }
            }
        }
        //绑定商品总费用
        public string GetTotalPrice(string strTotailPrice)
        {
            return ccObj.VarStr(strTotailPrice, 2);
        }
        public string GetStatus(int IntOrderID)
        {
            string strSql = "select (case IsConfirm when '0' then '未确认' when '1' then '已确认' end ) as IsConfirm";
            strSql += ",(case IsSend when '0' then '未发货' when '1' then '已发货' end ) as IsSend";
            strSql += " from OrderInfo where OrderID=" + IntOrderID;
            DataTable dsTable = dbObj.GetDataSetStr(strSql, "tbOI");
            return (dsTable.Rows[0][0].ToString() + "|" + dsTable.Rows[0][1].ToString());
        }
        //获取订单信息
        public OrderProperty GetOrderInfo()
        {
            string strSql = "select * from OrderInfo where OrderID=" + Convert.ToInt32(Request["OrderID"].Trim());
            DataTable dsTable = dbObj.GetDataSetStr(strSql, "tbOI");
            order.OrderNo = Convert.ToInt32(Request["OrderID"].Trim());                   //订单编号
            order.OrderTime = Convert.ToDateTime(dsTable.Rows[0]["OrderDate"].ToString());//下单时间
            order.ProductPrice = float.Parse(dsTable.Rows[0]["OrderTotal"].ToString());     //商品总金额
            order.ShipPrice = float.Parse(dsTable.Rows[0]["FreightFare"].ToString());         //商品运费
            order.TotalPrice = float.Parse(dsTable.Rows[0]["TotalPrice"].ToString());     //订单总金额
            order.ShipType = dsTable.Rows[0]["ShipType"].ToString();                      //运输类型
            order.ReceiverAddress = dsTable.Rows[0]["ReceiverAddress"].ToString();        //购货人地址
            order.ReceiverEmail = dsTable.Rows[0]["ReceiverEmail"].ToString();            //Email地址
            order.ReceiverName = dsTable.Rows[0]["ReceiverName"].ToString();              //收货人姓名
            order.ReceiverPhone = dsTable.Rows[0]["ReceiverPhone"].ToString();            //联系人电话
            order.ReceiverPostcode = dsTable.Rows[0]["ReceiverPostcode"].ToString();      //邮政编码
            order.ShipType = dsTable.Rows[0]["ShipType"].ToString();
            return (order);
        }
        //修改订单表中订单状态
        protected void btnSave_Click(object sender, EventArgs e)
        {
            int blConfirm = Convert.ToInt32(this.chkConfirm.Checked); //是否被确认
            int blSend = Convert.ToInt32(this.chkConsignment.Checked);//是否已发货
            int IntAdminID = Convert.ToInt32(Session["AID"]);
                                                                         
            string strSql = "update OrderInfo ";
            strSql += "  set IsConfirm='" + blConfirm + "',IsSend='" + blSend + "',AdminID='" + IntAdminID + "',ConfirmTime='" + DateTime.Now + "'";
            strSql += "where OrderID=" + Convert.ToInt32(Request["OrderID"].Trim());
            SqlCommand myCmd = dbObj.GetCommandStr(strSql);
            dbObj.ExecNonQuery(myCmd);
            if (blConfirm==1)
            {
                Response.Write(ccObj.MessageBox("修改成功！", "orderlist.aspx?OrderList=01&menu=2"));
            }
            else if (blSend==1)
            {
                Response.Write(ccObj.MessageBox("修改成功！", "orderlist.aspx?OrderList=11&menu=4"));
            }
        }
    }
}